package com.ztgf.shopping.api.client.audit;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.FileUtils;
import com.ztgf.file.models.bo.FileBO;
import com.ztgf.file.models.bo.FileInfoSaveBO;
import com.ztgf.common.utils.FileUtils;
import com.ztgf.order.models.service.purchase.IPurchaseInfoService;
import com.ztgf.shopping.models.bo.audit.AuditBO;
import com.ztgf.shopping.models.bo.audit.AuditImportPurchaseSaveBO;
import com.ztgf.shopping.models.bo.audit.AuditSaveBO;
import com.ztgf.shopping.models.bo.shop.ShopInfoAuditBO;
import com.ztgf.shopping.models.dto.audit.AuditDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.audit.IAuditService;
import com.ztgf.shopping.models.vo.audit.AuditTreeVO;
import com.ztgf.shopping.models.vo.audit.AuditVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/audit")
@Slf4j
@Api(value = "AuditController",tags = "审核表接口")
public class AuditApiController {
    @DubboReference
    private IAuditService iAuditService;
    @DubboReference
    private IPurchaseInfoService purchaseInfoService;

//    @DubboReference
//    private FastdfsUtils fastdfsUtils;


    @PostMapping("/getInfo")
    @ApiOperation(value = "根据id和类型查找审核详情",notes = "根据id和类型查找审核详情")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<List<AuditDTO>> getInfo(@RequestParam("id") Long id, Integer type){
        if(id == null || type == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        Wrapper<AuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("type", type);

        if (!type.equals(ShoppingCodeEnum.AUDIT_TYPE_LOGIN.getCode()) && !type.equals(ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode())){
            wrapper.eq("status", ShoppingCodeEnum.AUDIT_CREATE.getCode());
        }

        if (type.equals(ShoppingCodeEnum.AUDIT_TYPE_LOGIN.getCode())){
            //注册详情
            wrapper.eq("user_id", id);
        }else{
            //其他
            wrapper.eq("shop_id", id);
        }

        wrapper.orderBy("create_time", false);//倒序

        List<AuditDTO> auditDTOList =  iAuditService.selectList(wrapper);
        return new Result<>(auditDTOList);
    }

    @ApiOperation(value = "待审核列表",notes = "待审核列表")
    @PostMapping("/queryPageList")
    public Result<Page<AuditDTO>> queryPageList(ShopInfoAuditBO bo){
        Page<AuditDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());;
        Wrapper<AuditDTO> wrapper = new EntityWrapper<>();
        if (bo.getAuditType() != null){
            wrapper.eq("type", bo.getAuditType());
        }
        if (bo.getShopId() != null){
            wrapper.eq("shop_id", bo.getShopId());
        }
        wrapper.eq("status", ShoppingCodeEnum.AUDIT_CREATE.getCode());

        wrapper.orderBy("create_time", false);//倒序

        page = iAuditService.selectPage(page, wrapper);
        return new Result<>(page);
    }

    @ApiOperation(value = "根据审核表ID获取树",notes = "根据审核表ID获取树")
    @PostMapping("/queryTreeList")
    public Result<List<AuditTreeVO>> queryPageList(Long id){
        if (id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        return iAuditService.queryTreeList(id);
    }


    @ApiOperation(value = "保存审核信息",notes = "保存审核信息")
    @PostMapping("/save")
    public Result save(AuditSaveBO bo){
        return iAuditService.save(bo);
    }


    @ApiOperation(value = "获取审核列表",notes = "获取审核列表")
    @PostMapping("/queryPageListNew")
    public Result<Page<AuditVO>> queryPageListNew(AuditBO bo){
        Page<AuditVO> page = iAuditService.queryPageListNew(bo);
        return new Result<>(page);
    }

    @ApiOperation(value = "根据id获取审核记录",notes = "根据id获取审核记录")
    @PostMapping("/get")
    public Result<AuditVO> get(Long id){
        return iAuditService.get(id);
    }

    @ApiOperation(value = "导入询价单",notes = "导入询价单")
    @PostMapping("/importPurchase")
    @SneakyThrows
    public Result importPurchase(AuditImportPurchaseSaveBO bo, @RequestParam("file") MultipartFile file){
        if (null == file || file.isEmpty()) {
            return new Result<>(CodeEnum.FAIL_FILE_400000);
        }
        String originalFilename = file.getOriginalFilename();
        originalFilename = FileUtils.checkCompatible(originalFilename);
        String fileName = FileUtils.getFileName(originalFilename);
        String fileExtName = FileUtils.getFileExtName(originalFilename);
        byte[] input = file.getBytes();

        FileBO fileBO = new FileBO();
        fileBO.setFileName(fileName);
        fileBO.setFileExtName(fileExtName);
        fileBO.setInputs(input);
        fileBO.setFileSize(file.getSize());
        List<FileBO> list = new ArrayList<FileBO>();
        list.add(fileBO);
        FileInfoSaveBO saveBO = new FileInfoSaveBO();
        saveBO.setFileBOS(list);
        saveBO.setShopId(bo.getShopId());
        saveBO.setPlatId(bo.getCreateUserId());
        saveBO.setBusinessType(9);//上传的excel模板类型
        return iAuditService.saveImportPurchase(bo,input,saveBO);
    }
}
